package hot100;

import java.util.Arrays;

public class Solution406 {

    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
        int[][] ans = new int[people.length][];
        for (int i = 0; i < people.length; i++) {
            int space = people[i][1] + 1;
            for (int j = 0; j < ans.length ; j++) {
                if (ans[j] == null) {
                    space--;
                }
                if (space == 0) {
                    ans[j] = people[i];
                    break;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution406 solution406 = new Solution406();
        System.out.println(Arrays
            .deepToString(solution406.reconstructQueue(new int[][]{{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}})));
    }
}
